import urllib.request
import urllib.parse
import http.cookiejar

url="https://beacon.tingyun.com/xhr1?pvid=741a1858-3e7e-451b-bf2a-09122aeacbfe&ref=https%3A%2F%2Fwww.csdn.net%2F&referrer=https%3A%2F%2Fpassport.csdn.net%2Flogin&key=5raC_g4Hadc&v=1.7.7&av=1.7.7&did=085780ed-0c67-473a-9e5d-0db6b4a6ce04&sid=1a3c5b96-7937-45cc-82e7-57dd4baadb3d&__r=1547533926671"
postdata=urllib.parse.urlencode({
		"all":"mwqyykl",
		"pwd":"maowenqing"
	}).encode('utf-8')
req = urllib.request.Request(url,postdata)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36')
#添加cookie
cjar = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cjar))
urllib.request.install_opener(opener)
data = urllib.request.urlopen(req).read()
file1 = open('C:/Users/will.mao/Desktop/爬虫demo/TestCookieLogin.html',"wb")
print(len(data))
file1.write(data)
file1.close()


url2="https://www.csdn.net/nav/watchers"
req2 = urllib.request.Request(url2)
req2.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36')
data2 = urllib.request.urlopen(req2).read()
file2 = open('C:/Users/will.mao/Desktop/爬虫demo/TestCookieLogin2.html',"wb")
file2.write(data2)
file2.close()